package com.zqc

import com.android.build.api.transform.Context
import com.android.build.api.transform.DirectoryInput
import com.android.build.api.transform.JarInput
import com.android.build.api.transform.QualifiedContent
import com.android.build.api.transform.Transform
import com.android.build.api.transform.TransformException
import com.android.build.api.transform.TransformInput
import com.android.build.api.transform.TransformOutputProvider
import com.android.build.gradle.AppExtension
import com.android.build.gradle.internal.pipeline.TransformManager
import org.gradle.api.Plugin
import org.gradle.api.Project

import org.apache.commons.io.FileUtils
import org.apache.commons.codec.digest.DigestUtils
import com.android.build.api.transform.Format;

class MyPlugin extends Transform implements Plugin<Project> {

  private Project project
  @Override
  String getName() {
    return "MyPlugin"
  }

  @Override
  Set<QualifiedContent.ContentType> getInputTypes() {
    return TransformManager.CONTENT_CLASS
  }

  @Override
  Set<QualifiedContent.Scope> getScopes() {
    return TransformManager.SCOPE_FULL_PROJECT
  }

  @Override
  boolean isIncremental() {
    return false
  }

  @Override
  void apply(Project project) {
    this.project = project
    project.logger.error "========== MyPlugin apply==========="
    def android = project.extensions.getByType(AppExtension)
    android.registerTransform(this)
  }

  @Override
  void transform(Context context, Collection<TransformInput> inputs, Collection<TransformInput> referencedInputs, TransformOutputProvider outputProvider, boolean isIncremental) throws IOException, TransformException, InterruptedException {
    // Transform的inputs有两种类型，一种是目录，一种是jar包，要分开遍历
    project.logger.error "========== MyPlugin transform==========="

    inputs.each { TransformInput input ->
      //对类型为“文件夹”的input进行遍历
      input.directoryInputs.each { DirectoryInput directoryInput ->
        //文件夹里面包含的是我们手写的类以及R.class、BuildConfig.class以及R$XXX.class等
        project.logger.error "========== MyInject.injectDir ==========="
        MyInject.injectDir(project, directoryInput.file.absolutePath, "com/zqc/myp")
        // 获取output目录
        def dest = outputProvider.getContentLocation(directoryInput.name,
            directoryInput.contentTypes, directoryInput.scopes,
            Format.DIRECTORY)

        // 将input的目录复制到output指定目录
        FileUtils.copyDirectory(directoryInput.file, dest)
      }
      //对类型为jar文件的input进行遍历
      input.jarInputs.each { JarInput jarInput ->

        //jar文件一般是第三方依赖库jar文件

        // 重命名输出文件（同目录copyFile会冲突）
        def jarName = jarInput.name
        def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath())
        if (jarName.endsWith(".jar")) {
          jarName = jarName.substring(0, jarName.length() - 4)
        }
        //生成输出路径
        def dest = outputProvider.getContentLocation(jarName + md5Name,
            jarInput.contentTypes, jarInput.scopes, Format.JAR)
        //将输入内容复制到输出
        FileUtils.copyFile(jarInput.file, dest)
      }
    }
  }
}